<template>
  <div class="personal">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>个人中心</span>
      </div>
      <div class="content">
        <p>
          管理员ID: <span>{{ id }}</span>
        </p>
        <p>
          账号: <span>{{ account }}</span>
        </p>
        <p>
          用户组: <span>{{ userGroup }}</span>
        </p>
        <p>
          创建时间: <span>{{ ctime | formatDate }}</span>
        </p>
        <p>
          管理员头像:
          <el-upload
            class="avatar-uploader"
            :action="action"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </p>
      </div>
    </el-card>
  </div>
</template>

<script>
import { userInfo,avatarEdit } from "../../api/user";
import dayjs from "dayjs";
export default {
  data() {
    return {
      action:'http://127.0.0.1:5000/users/avatar_upload',
      id: "",
      account: "",
      userGroup: "",
      ctime: "",
      imageUrl: "",
    };
  },
  created() {
    this.loadInfo();
  },
  methods: {
    async loadInfo() {
      let res = await userInfo();
      let { id, account, userGroup, ctime, imgUrl } = res;
      this.id = id;
      this.account = account;
      this.userGroup = userGroup;
      this.ctime = ctime;
      this.imageUrl = imgUrl
    },
    //替换之前的头像
    async handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.imageUrl = res.imgUrl
      if(res.code===0){
        let imgData = await avatarEdit({imgUrl:res.imgUrl})
        console.log(imgData)
        if(imgData.code === 0){
          //头像已经修改,需要重新渲染
          this.$bus.$emit('reUpload','已经修改成功了')
        }
      }
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg" || "image/webp";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
  filters: {
    formatDate(val) {
      return dayjs(val).format("YYYY/MM/DD");
    },
  },
};
</script>

<style lang="less" scoped>
.personal {
  background-color: #fff;
  .content {
    p {
      line-height: 70px;
      display: flex;
      align-items: center;
      border-bottom: 1px solid #ccc;
      &:last-child {
        border-bottom: none;
      }
      span {
        margin-left: 10px;
      }
    }
  }
}
/deep/.avatar-uploader .el-upload {
  margin: 20px 0 0 20px;
  border: 1px solid #ccc;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>